﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using UnityEngine;

namespace UniRx
{
	// Token: 0x020003CB RID: 971
	public abstract class LazyTask
	{
		// Token: 0x17000166 RID: 358
		// (get) Token: 0x060013BF RID: 5055 RVA: 0x00059488 File Offset: 0x00057888
		// (set) Token: 0x060013C0 RID: 5056 RVA: 0x00059490 File Offset: 0x00057890
		public LazyTask.TaskStatus Status { get; protected set; }

		// Token: 0x060013C1 RID: 5057
		public abstract Coroutine Start();

		// Token: 0x060013C2 RID: 5058 RVA: 0x00059499 File Offset: 0x00057899
		public void Cancel()
		{
			if (this.Status == LazyTask.TaskStatus.WaitingToRun || this.Status == LazyTask.TaskStatus.Running)
			{
				this.Status = LazyTask.TaskStatus.Canceled;
				this.cancellation.Dispose();
			}
		}

		// Token: 0x060013C3 RID: 5059 RVA: 0x000594C4 File Offset: 0x000578C4
		public static LazyTask<T> FromResult<T>(T value)
		{
			return LazyTask<T>.FromResult(value);
		}

		// Token: 0x060013C4 RID: 5060 RVA: 0x000594CC File Offset: 0x000578CC
		public static Coroutine WhenAll(params LazyTask[] tasks)
		{
			return LazyTask.WhenAll(tasks.AsEnumerable<LazyTask>());
		}

		// Token: 0x060013C5 RID: 5061 RVA: 0x000594DC File Offset: 0x000578DC
		public static Coroutine WhenAll(IEnumerable<LazyTask> tasks)
		{
			Coroutine[] coroutines = (from x in tasks
			select x.Start()).ToArray<Coroutine>();
			return MainThreadDispatcher.StartCoroutine(LazyTask.WhenAllCore(coroutines));
		}

		// Token: 0x060013C6 RID: 5062 RVA: 0x00059520 File Offset: 0x00057920
		private static IEnumerator WhenAllCore(Coroutine[] coroutines)
		{
			foreach (Coroutine item in coroutines)
			{
				yield return item;
			}
			yield break;
		}

		// Token: 0x04000B82 RID: 2946
		protected readonly BooleanDisposable cancellation = new BooleanDisposable();

		// Token: 0x020003CC RID: 972
		public enum TaskStatus
		{
			// Token: 0x04000B85 RID: 2949
			WaitingToRun,
			// Token: 0x04000B86 RID: 2950
			Running,
			// Token: 0x04000B87 RID: 2951
			Completed,
			// Token: 0x04000B88 RID: 2952
			Canceled,
			// Token: 0x04000B89 RID: 2953
			Faulted
		}
	}
}
